home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / utility / smult64.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  2KB  |  102 lines

  1. /*
  2.     $Id: smult64.c,v 1.1 1996/08/31 12:58:13 aros Exp $
  3.     $Log: smult64.c,v $
  4.     Revision 1.1  1996/08/31 12:58:13  aros
  5.     Merged in/modified for FreeBSD.
  6.  
  7.     Desc: Signed 64 bit multiplication function.
  8.     Lang: english
  9. */
  10. #include "utility_intern.h"
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.         #include <clib/utility_protos.h>
  16.  
  17.         __AROS_LH2(LONG, SMult64,
  18.  
  19. /*  SYNOPSIS */
  20.         __AROS_LHA(long, arg1, D0),
  21.         __AROS_LHA(long, arg2, D1),
  22.  
  23. /*  LOCATION */
  24.         struct UtilityBase *, UtilityBase, 33, Utility)
  25.  
  26. /*  FUNCTION
  27.         Compute the signed 64-bit product of arg1 * arg2.
  28.  
  29.     INPUTS
  30.         arg1, arg2  -   32 bit signed numbers.
  31.  
  32.     RESULT
  33.         arg1 * arg2
  34.  
  35.     NOTES
  36.  
  37.     EXAMPLE
  38.  
  39.     BUGS
  40.         There is a problem under the current system in that it is very
  41.         hard to return a 64-bit value.
  42.  
  43.     SEE ALSO
  44.         utility/SMult32(), utility/UMult32(), utility/UMult64()
  45.  
  46.     INTERNALS
  47.         This is essentially SMult32(), but without the code to calculate
  48.         the product of the high 32 bits of the multiplicands.
  49.  
  50.         In fact all that is added is the 2^32 * ac term (see docs for
  51.             SMult32().
  52.  
  53.     HISTORY
  54.         29-10-95    digulla automatically created from
  55.                             utility_lib.fd and clib/utility_protos.h
  56.         18-08-96    iaint   Modified SMult32().
  57.  
  58. *****************************************************************************/
  59. {
  60.     __AROS_FUNC_INIT
  61.  
  62. #ifdef HAS_64BITMULS
  63.     return arg1 * arg2;
  64. #else
  65.  
  66.     long long product;
  67.     WORD a0, a1, b0, b1;
  68.     BOOL neg;
  69.  
  70.     /* Fix everything up so that -ve signs don't vanish */
  71.     if(arg1 < 0)
  72.     {
  73.         neg = 1; arg1 = -arg1;
  74.     }
  75.     else
  76.         neg = 0;
  77.  
  78.     if(arg2 <= 0)
  79.     {
  80.         neg ^= 1; arg2 = -arg2;
  81.     }
  82.  
  83.     a0 = arg1 & 0xFFFF;
  84.     a1 = (arg1 >> 16) & 0xFFFF;
  85.  
  86.     b0 = arg2 & 0xFFFF;
  87.     b1 = (arg2 >> 16) & 0xFFFF;
  88.  
  89.     /* In case numbers are small */
  90.     if(a1 && b1)
  91.         product = (long long)(a1 * b1) << 32;
  92.     else
  93.         product = 0;
  94.  
  95.     product += (((a1 * b0) + (a0 * b1)) << 16) + (a0 * b0);
  96.  
  97.     return (neg ? -product : product);
  98. #endif
  99.  
  100.     __AROS_FUNC_EXIT
  101. } /* SMult64 */
  102.